package com.dms.modules.message.service.impl;

import com.dms.modules.message.dto.BusinessMessageDTO;
import com.dms.modules.message.dto.MessageDTO;
import com.dms.modules.message.dto.MessageDetailDTO;
import com.dms.modules.message.enums.BusinessMessageType;
import com.dms.modules.message.service.BusinessMessageService;
import com.dms.modules.message.service.MessageService;
import com.dms.modules.message.service.MessageTemplateService;
import com.dms.modules.message.websocket.MessageWebSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Lazy;

import java.util.Collections;

@Slf4j
@Service
public class BusinessMessageServiceImpl implements BusinessMessageService {

    @Autowired
    private MessageService messageService;

    @Autowired
    private MessageTemplateService messageTemplateService;

    @Autowired
    @Lazy
    private MessageWebSocket messageWebSocket;

    @Override
    public void sendMessage(BusinessMessageDTO message) {
        sendBusinessMessage(message);
    }

    @Override
    public void broadcastMessage(BusinessMessageDTO message) {
        // 实现广播消息的逻辑
    }

    @Override
    public void sendBusinessMessage(BusinessMessageDTO businessMessage) {
        try {
            // 1. 构建消息DTO
            MessageDTO messageDTO = new MessageDTO();
            messageDTO.setType(getMessageType(businessMessage.getType()));
            messageDTO.setTargetType(2); // 指定用户
            messageDTO.setTargetIds(Collections.singletonList(businessMessage.getUserId()));
            
            // 2. 获取消息内容
            String content = messageTemplateService.getContent(
                businessMessage.getType(),
                businessMessage.getParams()
            );
            
            messageDTO.setTitle(businessMessage.getType().getDescription());
            messageDTO.setContent(content);
            
            // 3. 保存消息
            Long messageId = messageService.sendMessage(messageDTO);
            
            // 4. 发送WebSocket通知
            if (messageId != null) {
                MessageDetailDTO detailDTO = new MessageDetailDTO();
                detailDTO.setId(messageId);
                detailDTO.setTitle(messageDTO.getTitle());
                detailDTO.setContent(messageDTO.getContent());
                detailDTO.setType(messageDTO.getType());
                detailDTO.setUserId(businessMessage.getUserId());
                messageWebSocket.sendMessage(businessMessage.getUserId(), detailDTO);
            }
        } catch (Exception e) {
            log.error("发送业务消息失败: message={}, error={}", 
                businessMessage, e.getMessage(), e);
        }
    }

    @Override
    public int getMessageType(BusinessMessageType type) {
        if (type.getCode() >= 200 && type.getCode() < 300) {
            return 2; // 订单通知
        } else if (type.getCode() >= 300 && type.getCode() < 400) {
            return 3; // 商品通知
        }
        return 1; // 默认系统通知
    }
} 